package com.vieewer.btdemo.view.gdmap;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.vieewer.btdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 步行路线详情页adapter
 */
public class GdSegmentListAdapter extends RecyclerView.Adapter<GdSegmentListAdapter.ViewHolder> {

    private Context mContext;
    private List<GdMapPlanBean> mItemList = new ArrayList<GdMapPlanBean>();

    public GdSegmentListAdapter(Context context, List<GdMapPlanBean> steps) {
        mContext = context;
        mItemList.add(new GdMapPlanBean());
        for (GdMapPlanBean walkStep : steps) {
            mItemList.add(walkStep);
        }
        mItemList.add(new GdMapPlanBean());
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bus_segment, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        bindViews(holder, position);
    }

    private void bindViews(ViewHolder holder, int position) {
        final GdMapPlanBean item = mItemList.get(position);
        if (position == 0) {
            holder.dirIcon.setImageResource(R.drawable.dir_start);
            holder.lineName.setText("出发");
            holder.dirUp.setVisibility(View.INVISIBLE);
            holder.dirDown.setVisibility(View.VISIBLE);
            holder.splitLine.setVisibility(View.INVISIBLE);
        } else if (position == mItemList.size() - 1) {
            holder.dirIcon.setImageResource(R.drawable.dir_end);
            holder.lineName.setText("到达终点");
            holder.dirUp.setVisibility(View.VISIBLE);
            holder.dirDown.setVisibility(View.INVISIBLE);
        } else {
            holder.splitLine.setVisibility(View.VISIBLE);
            holder.dirUp.setVisibility(View.VISIBLE);
            holder.dirDown.setVisibility(View.VISIBLE);
            String actionName = item.getAction();
            int resID = AMapUtil.getWalkActionID(actionName);
            holder.dirIcon.setImageResource(resID);
            holder.lineName.setText(item.getDes() + ".." + item.getOrien());
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mItemList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView lineName;
        private ImageView dirIcon;
        private ImageView dirUp;
        private ImageView dirDown;
        private ImageView splitLine;

        public ViewHolder(View itemView) {
            super(itemView);

            lineName = (TextView) itemView.findViewById(R.id.bus_line_name);
            dirIcon = (ImageView) itemView.findViewById(R.id.bus_dir_icon);
            dirUp = (ImageView) itemView.findViewById(R.id.bus_dir_icon_up);
            dirDown = (ImageView) itemView.findViewById(R.id.bus_dir_icon_down);
            splitLine = (ImageView) itemView.findViewById(R.id.bus_seg_split_line);

        }
    }

}
